using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Fine.CRM.Common
{
    public class EncDec
    {
        private static string defaultPassword;

        static EncDec()
        {
            EncDec.defaultPassword = "7a5a64brEgaceqenuyegac7era3Ape6aWatrewegeka94waqegayathudrebuc7t";
        }

        public EncDec()
        {
        }

        public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            MemoryStream memoryStream = new MemoryStream();
            Rijndael key = Rijndael.Create();
            key.Key = Key;
            key.IV = IV;
            CryptoStream cryptoStream = new CryptoStream(memoryStream, key.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(cipherData, 0, (int)cipherData.Length);
            cryptoStream.Close();
            byte[] array = memoryStream.ToArray();
            byte[] numArray = array;
            return numArray;
        }

        public static string Decrypt(string cipherText)
        {
            string str = EncDec.Decrypt(cipherText, EncDec.defaultPassword);
            return str;
        }

        public static string Decrypt(string cipherText, string Password)
        {
            byte[] numArray = Convert.FromBase64String(cipherText);
            byte[] numArray1 = new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 };
            PasswordDeriveBytes passwordDeriveByte = new PasswordDeriveBytes(Password, numArray1);
            byte[] numArray2 = EncDec.Decrypt(numArray, passwordDeriveByte.GetBytes(32), passwordDeriveByte.GetBytes(16));
            string str = Encoding.Unicode.GetString(numArray2);
            return str;
        }

        public static byte[] Decrypt(byte[] cipherData, string Password)
        {
            byte[] numArray = new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 };
            PasswordDeriveBytes passwordDeriveByte = new PasswordDeriveBytes(Password, numArray);
            byte[] numArray1 = EncDec.Decrypt(cipherData, passwordDeriveByte.GetBytes(32), passwordDeriveByte.GetBytes(16));
            return numArray1;
        }

        public static void Decrypt(string fileIn, string fileOut, string Password)
        {
            bool flag;
            FileStream fileStream = new FileStream(fileIn, FileMode.Open, FileAccess.Read);
            FileStream fileStream1 = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);
            byte[] numArray = new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 };
            PasswordDeriveBytes passwordDeriveByte = new PasswordDeriveBytes(Password, numArray);
            Rijndael bytes = Rijndael.Create();
            bytes.Key = passwordDeriveByte.GetBytes(32);
            bytes.IV = passwordDeriveByte.GetBytes(16);
            CryptoStream cryptoStream = new CryptoStream(fileStream1, bytes.CreateDecryptor(), CryptoStreamMode.Write);
            int num = 0x1000;
            byte[] numArray1 = new byte[num];
            do
            {
                int num1 = fileStream.Read(numArray1, 0, num);
                cryptoStream.Write(numArray1, 0, num1);
                flag = num1 != 0;
            }
            while (flag);
            cryptoStream.Close();
            fileStream.Close();
        }

        public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream memoryStream = new MemoryStream();
            Rijndael key = Rijndael.Create();
            key.Key = Key;
            key.IV = IV;
            CryptoStream cryptoStream = new CryptoStream(memoryStream, key.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(clearData, 0, (int)clearData.Length);
            cryptoStream.Close();
            byte[] array = memoryStream.ToArray();
            byte[] numArray = array;
            return numArray;
        }

        public static string Encrypt(string clearText)
        {
            string str = EncDec.Encrypt(clearText, EncDec.defaultPassword);
            return str;
        }

        public static string Encrypt(string clearText, string Password)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(clearText);
            byte[] numArray = new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 };
            PasswordDeriveBytes passwordDeriveByte = new PasswordDeriveBytes(Password, numArray);
            byte[] numArray1 = EncDec.Encrypt(bytes, passwordDeriveByte.GetBytes(32), passwordDeriveByte.GetBytes(16));
            string base64String = Convert.ToBase64String(numArray1);
            return base64String;
        }

        public static byte[] Encrypt(byte[] clearData, string Password)
        {
            byte[] numArray = new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 };
            PasswordDeriveBytes passwordDeriveByte = new PasswordDeriveBytes(Password, numArray);
            byte[] numArray1 = EncDec.Encrypt(clearData, passwordDeriveByte.GetBytes(32), passwordDeriveByte.GetBytes(16));
            return numArray1;
        }

        public static void Encrypt(string fileIn, string fileOut, string Password)
        {
            bool flag;
            FileStream fileStream = new FileStream(fileIn, FileMode.Open, FileAccess.Read);
            FileStream fileStream1 = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);
            byte[] numArray = new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 };
            PasswordDeriveBytes passwordDeriveByte = new PasswordDeriveBytes(Password, numArray);
            Rijndael bytes = Rijndael.Create();
            bytes.Key = passwordDeriveByte.GetBytes(32);
            bytes.IV = passwordDeriveByte.GetBytes(16);
            CryptoStream cryptoStream = new CryptoStream(fileStream1, bytes.CreateEncryptor(), CryptoStreamMode.Write);
            int num = 0x1000;
            byte[] numArray1 = new byte[num];
            do
            {
                int num1 = fileStream.Read(numArray1, 0, num);
                cryptoStream.Write(numArray1, 0, num1);
                flag = num1 != 0;
            }
            while (flag);
            cryptoStream.Close();
            fileStream.Close();
        }
    }
}